{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataFrame类型\n",
    "DataFrame是一个多维数据类型。因为通常使用二维数据，因此，我们可以将DataFrame理解成类似excel的表格型数据，由多列组成，每个列的类型可以不同。  \n",
    "因为DataFrame是多维数据类型，因此，DataFrame既有行索引，也有列索引。\n",
    "## 创建方式\n",
    "我们可以使用如下的方式创建（初始化）DataFrame类型的对象（常用）：\n",
    "* 二维数组结构（列表,ndarray数组，DataFrame等）类型。\n",
    "* 字典类型，key为列名，value为一维数组结构（列表，ndarray数组,Series等）。\n",
    "\n",
    "说明：\n",
    "* 如果没有显式指定行与列索引，则会自动生成以0开始的整数值索引。我们可以在创建DataFrame对象时，通过index与columns参数指定。\n",
    "* 可以通过head，tail访问前 / 后N行记录（数据）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3         4\n",
      "0  0.036476  0.890412  0.980921  0.059942  0.890546\n",
      "1  0.576901  0.742480  0.630184  0.581842  0.020439\n",
      "2  0.210027  0.544685  0.769115  0.250695  0.285896\n"
     ]
    }
   ],
   "source": [
    "# 使用二维数据结构创建DataFrame\n",
    "df = pd.DataFrame(np.random.rand(3, 5))\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>109</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>125</td>\n",
       "      <td>123</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    北京   天津   上海\n",
       "0  100  109   39\n",
       "1  200  203   90\n",
       "2  125  123  300"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 使用字典来创建DataFrame。一个键值对为一列。key指定列索引，value指定该列的值。\n",
    "df = pd.DataFrame({\"北京\":[100, 200, 125], \"天津\":[109, 203, 123], \"上海\":[39, 90, 300]})\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>109</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    北京   天津  上海\n",
       "0  100  109  39\n",
       "1  200  203  90"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>200</td>\n",
       "      <td>203</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>125</td>\n",
       "      <td>123</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    北京   天津   上海\n",
       "1  200  203   90\n",
       "2  125  123  300"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 显示前（后）N条记录\n",
    "display(df.head(2))\n",
    "display(df.tail(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "      <th>沈阳</th>\n",
       "      <th>广州</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>地区1</th>\n",
       "      <td>0.852395</td>\n",
       "      <td>0.975006</td>\n",
       "      <td>0.884853</td>\n",
       "      <td>0.359508</td>\n",
       "      <td>0.598859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区2</th>\n",
       "      <td>0.354796</td>\n",
       "      <td>0.340190</td>\n",
       "      <td>0.178081</td>\n",
       "      <td>0.237694</td>\n",
       "      <td>0.044862</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区3</th>\n",
       "      <td>0.505431</td>\n",
       "      <td>0.376252</td>\n",
       "      <td>0.592805</td>\n",
       "      <td>0.629942</td>\n",
       "      <td>0.142600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           北京        天津        上海        沈阳        广州\n",
       "地区1  0.852395  0.975006  0.884853  0.359508  0.598859\n",
       "地区2  0.354796  0.340190  0.178081  0.237694  0.044862\n",
       "地区3  0.505431  0.376252  0.592805  0.629942  0.142600"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建DataFrame，指定行，列索引。\n",
    "df = pd.DataFrame(np.random.rand(3, 5), index=[\"地区1\", \"地区2\", \"地区3\"], columns=[\"北京\",\"天津\", \"上海\",\"沈阳\", \"广州\"])\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 相关属性\n",
    "* index\n",
    "* columns\n",
    "* values\n",
    "* shape\n",
    "* ndim\n",
    "* dtypes\n",
    "\n",
    "说明：\n",
    "* 可以通过index访问行索引，columns访问列索引，values访问数据，其中index与columns也可以进行设置（修改）。\n",
    "* 可以为DataFrame的index与columns属性指定name属性值。\n",
    "* DataFrame的数据不能超过二维。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "      <th>沈阳</th>\n",
       "      <th>广州</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>地区1</th>\n",
       "      <td>0.933841</td>\n",
       "      <td>0.946380</td>\n",
       "      <td>0.602297</td>\n",
       "      <td>0.387766</td>\n",
       "      <td>0.363188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区2</th>\n",
       "      <td>0.204345</td>\n",
       "      <td>0.276765</td>\n",
       "      <td>0.246536</td>\n",
       "      <td>0.173608</td>\n",
       "      <td>0.966610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区3</th>\n",
       "      <td>0.957013</td>\n",
       "      <td>0.597974</td>\n",
       "      <td>0.731301</td>\n",
       "      <td>0.340385</td>\n",
       "      <td>0.092056</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           北京        天津        上海        沈阳        广州\n",
       "地区1  0.933841  0.946380  0.602297  0.387766  0.363188\n",
       "地区2  0.204345  0.276765  0.246536  0.173608  0.966610\n",
       "地区3  0.957013  0.597974  0.731301  0.340385  0.092056"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0.9338413 , 0.94637988, 0.60229666, 0.38776628, 0.363188  ],\n",
       "       [0.20434528, 0.27676506, 0.24653588, 0.173608  , 0.96660969],\n",
       "       [0.9570126 , 0.59797368, 0.73130075, 0.34038522, 0.0920556 ]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Index(['地区1', '地区2', '地区3'], dtype='object')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Index(['北京', '天津', '上海', '沈阳', '广州'], dtype='object')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(3, 5), index=[\"地区1\", \"地区2\", \"地区3\"], columns=[\"北京\",\"天津\", \"上海\",\"沈阳\", \"广州\"])\n",
    "display(df)\n",
    "display(df.values)  # 返回DataFrame关联的ndarray数组\n",
    "display(df.index)   # 返回行索引\n",
    "display(df.columns)  # 返回列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "北京    float64\n",
       "天津    float64\n",
       "上海    float64\n",
       "沈阳    float64\n",
       "广州    float64\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 返回形状\n",
    "display(df.shape)\n",
    "# 返回维度\n",
    "display(df.ndim)\n",
    "# 返回各列的类型信息。\n",
    "display(df.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>columns_name</th>\n",
       "      <th>北京</th>\n",
       "      <th>天津</th>\n",
       "      <th>上海</th>\n",
       "      <th>沈阳</th>\n",
       "      <th>广州</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index_name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>地区1</th>\n",
       "      <td>0.933841</td>\n",
       "      <td>0.946380</td>\n",
       "      <td>0.602297</td>\n",
       "      <td>0.387766</td>\n",
       "      <td>0.363188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区2</th>\n",
       "      <td>0.204345</td>\n",
       "      <td>0.276765</td>\n",
       "      <td>0.246536</td>\n",
       "      <td>0.173608</td>\n",
       "      <td>0.966610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>地区3</th>\n",
       "      <td>0.957013</td>\n",
       "      <td>0.597974</td>\n",
       "      <td>0.731301</td>\n",
       "      <td>0.340385</td>\n",
       "      <td>0.092056</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "columns_name        北京        天津        上海        沈阳        广州\n",
       "index_name                                                    \n",
       "地区1           0.933841  0.946380  0.602297  0.387766  0.363188\n",
       "地区2           0.204345  0.276765  0.246536  0.173608  0.966610\n",
       "地区3           0.957013  0.597974  0.731301  0.340385  0.092056"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# df.index = [\"r1\", \"r2\", \"r3\"]\n",
    "df.index.name = \"index_name\"\n",
    "df.columns.name = \"columns_name\"\n",
    "display(df)\n",
    "# 错误，超过了2维。\n",
    "# df_more_than2d = pd.DataFrame(np.random.rand(3, 3, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame相关操作\n",
    "假设df为DataFrame类型的对象。  \n",
    "### 列操作\n",
    "* 获取列【哪个更好些？】\n",
    " + df\\[列索引\\]\n",
    " + df.列索引\n",
    "* 增加（修改）列：df\\[列索引\\] = 列数据\n",
    "* 删除列\n",
    " + del df\\[列索引\\]\n",
    " + df.pop(列索引)\n",
    " + df.drop(列索引或数组)\n",
    "\n",
    "### 行操作\n",
    "* 获取行\n",
    " + df.loc 根据标签进行索引。\n",
    " + df.iloc 根据位置进行索引。\n",
    " + df.ix 混合索引。先根据标签索引，如果没有找到，则根据位置进行索引（前提是标签不是数值类型）。【已不建议使用】\n",
    "* 增加行：append【多次使用append增加行会比连接计算量更大，可考虑使用pd.concat来代替。】\n",
    "* 删除行\n",
    " + df.drop(行索引或数组)\n",
    "\n",
    "### 行列混合操作：\n",
    "* 先获取行，再获取列。\n",
    "* 先获取列，在获取行。\n",
    "\n",
    "说明：\n",
    "* drop方法既可以删除行，也可以删除列，通过axis指定轴方向。【可以原地修改，也可以返回修改之后的结果。】\n",
    "* 通过df\\[索引\\]访问是对列进行操作。\n",
    "* 通过df\\[切片\\]访问是对行进行操作。【先按标签，然后按索引访问。如果标签是数值类型，则仅会按标签进行匹配。】\n",
    "* 通过布尔索引是对行进行操作。\n",
    "* 通过数组索引是对列进行操作。\n",
    "\n",
    "## <font color=\"green\">在对行或列进行操作时，如果针对多行（列）进行操作？如果是不连续的呢？</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>h</th>\n",
       "      <td>0.724381</td>\n",
       "      <td>0.162695</td>\n",
       "      <td>0.758338</td>\n",
       "      <td>0.834515</td>\n",
       "      <td>0.215246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.781026</td>\n",
       "      <td>0.819637</td>\n",
       "      <td>0.928551</td>\n",
       "      <td>0.709671</td>\n",
       "      <td>0.704519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>j</th>\n",
       "      <td>0.067835</td>\n",
       "      <td>0.659427</td>\n",
       "      <td>0.149030</td>\n",
       "      <td>0.325434</td>\n",
       "      <td>0.935286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.855434</td>\n",
       "      <td>0.693856</td>\n",
       "      <td>0.332076</td>\n",
       "      <td>0.858914</td>\n",
       "      <td>0.738961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.361972</td>\n",
       "      <td>0.272546</td>\n",
       "      <td>0.626712</td>\n",
       "      <td>0.518910</td>\n",
       "      <td>0.289262</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d         e\n",
       "h  0.724381  0.162695  0.758338  0.834515  0.215246\n",
       "i  0.781026  0.819637  0.928551  0.709671  0.704519\n",
       "j  0.067835  0.659427  0.149030  0.325434  0.935286\n",
       "k  0.855434  0.693856  0.332076  0.858914  0.738961\n",
       "l  0.361972  0.272546  0.626712  0.518910  0.289262"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "h    0.724381\n",
       "i    0.781026\n",
       "j    0.067835\n",
       "k    0.855434\n",
       "l    0.361972\n",
       "Name: a, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>h</th>\n",
       "      <td>0.724381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.781026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>j</th>\n",
       "      <td>0.067835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.855434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.361972</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a\n",
       "h  0.724381\n",
       "i  0.781026\n",
       "j  0.067835\n",
       "k  0.855434\n",
       "l  0.361972"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "      <th>f</th>\n",
       "      <th>g</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>h</th>\n",
       "      <td>0.724381</td>\n",
       "      <td>0.162695</td>\n",
       "      <td>0.758338</td>\n",
       "      <td>0.834515</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>0.887076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.781026</td>\n",
       "      <td>0.819637</td>\n",
       "      <td>0.928551</td>\n",
       "      <td>0.709671</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>1.600663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>j</th>\n",
       "      <td>0.067835</td>\n",
       "      <td>0.659427</td>\n",
       "      <td>0.149030</td>\n",
       "      <td>0.325434</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>0.727262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.855434</td>\n",
       "      <td>0.693856</td>\n",
       "      <td>0.332076</td>\n",
       "      <td>0.858914</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>1.549290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.361972</td>\n",
       "      <td>0.272546</td>\n",
       "      <td>0.626712</td>\n",
       "      <td>0.518910</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>0.634517</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d   e  f         g\n",
       "h  0.724381  0.162695  0.758338  0.834515   6  1  0.887076\n",
       "i  0.781026  0.819637  0.928551  0.709671   7  2  1.600663\n",
       "j  0.067835  0.659427  0.149030  0.325434   8  3  0.727262\n",
       "k  0.855434  0.693856  0.332076  0.858914   9  4  1.549290\n",
       "l  0.361972  0.272546  0.626712  0.518910  10  5  0.634517"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>f</th>\n",
       "      <th>g</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>h</th>\n",
       "      <td>0.162695</td>\n",
       "      <td>0.758338</td>\n",
       "      <td>0.834515</td>\n",
       "      <td>1</td>\n",
       "      <td>0.887076</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.819637</td>\n",
       "      <td>0.928551</td>\n",
       "      <td>0.709671</td>\n",
       "      <td>2</td>\n",
       "      <td>1.600663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>j</th>\n",
       "      <td>0.659427</td>\n",
       "      <td>0.149030</td>\n",
       "      <td>0.325434</td>\n",
       "      <td>3</td>\n",
       "      <td>0.727262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.693856</td>\n",
       "      <td>0.332076</td>\n",
       "      <td>0.858914</td>\n",
       "      <td>4</td>\n",
       "      <td>1.549290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.272546</td>\n",
       "      <td>0.626712</td>\n",
       "      <td>0.518910</td>\n",
       "      <td>5</td>\n",
       "      <td>0.634517</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          b         c         d  f         g\n",
       "h  0.162695  0.758338  0.834515  1  0.887076\n",
       "i  0.819637  0.928551  0.709671  2  1.600663\n",
       "j  0.659427  0.149030  0.325434  3  0.727262\n",
       "k  0.693856  0.332076  0.858914  4  1.549290\n",
       "l  0.272546  0.626712  0.518910  5  0.634517"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(5, 5), columns=list(\"abcde\"), index=list(\"hijkl\"))\n",
    "display(df)\n",
    "# 获取DataFrame的列。通过df[索引]的方式，永远是获取列，不会获取行。【索引永远被解析为是列索引。】\n",
    "display(df[\"a\"], type(df[\"a\"]))\n",
    "# 获取多个列（返回一个DataFrame，即使只选择一个列）\n",
    "display(df[[\"a\"]])\n",
    "\n",
    "# 获取列的第二种方式：(建议大家使用前者，因为不受特殊名称的限制。)\n",
    "# display(df.a)\n",
    "\n",
    "# 增加 / 修改列\n",
    "df[\"f\"] = [1, 2, 3, 4, 5]\n",
    "df[\"e\"] = [6, 7, 8, 9, 10]\n",
    "df[\"g\"] = df[\"a\"] + df[\"b\"]\n",
    "display(df)\n",
    "# 删除列\n",
    "# del df[\"e\"]\n",
    "# display(df.pop(\"e\"))\n",
    "#df.drop(\"e\", inplace=True, axis=1)\n",
    "# 可以指定标签数组，同时删除多列\n",
    "df.drop([\"a\", \"e\"], inplace=True, axis=1)\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>u</th>\n",
       "      <th>i</th>\n",
       "      <th>o</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.543405</td>\n",
       "      <td>0.278369</td>\n",
       "      <td>0.424518</td>\n",
       "      <td>0.844776</td>\n",
       "      <td>0.004719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.121569</td>\n",
       "      <td>0.670749</td>\n",
       "      <td>0.825853</td>\n",
       "      <td>0.136707</td>\n",
       "      <td>0.575093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.891322</td>\n",
       "      <td>0.209202</td>\n",
       "      <td>0.185328</td>\n",
       "      <td>0.108377</td>\n",
       "      <td>0.219697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.978624</td>\n",
       "      <td>0.811683</td>\n",
       "      <td>0.171941</td>\n",
       "      <td>0.816225</td>\n",
       "      <td>0.274074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>0.431704</td>\n",
       "      <td>0.940030</td>\n",
       "      <td>0.817649</td>\n",
       "      <td>0.336112</td>\n",
       "      <td>0.175410</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          y         u         i         o         p\n",
       "a  0.543405  0.278369  0.424518  0.844776  0.004719\n",
       "b  0.121569  0.670749  0.825853  0.136707  0.575093\n",
       "c  0.891322  0.209202  0.185328  0.108377  0.219697\n",
       "d  0.978624  0.811683  0.171941  0.816225  0.274074\n",
       "e  0.431704  0.940030  0.817649  0.336112  0.175410"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0.18532821955007506"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0.18532821955007506"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0.18532821955007506"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "y    0.891322\n",
       "u    0.209202\n",
       "i    0.185328\n",
       "o    0.108377\n",
       "p    0.219697\n",
       "Name: c, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>i</th>\n",
       "      <th>o</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.825853</td>\n",
       "      <td>0.136707</td>\n",
       "      <td>0.575093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.185328</td>\n",
       "      <td>0.108377</td>\n",
       "      <td>0.219697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.171941</td>\n",
       "      <td>0.816225</td>\n",
       "      <td>0.274074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          i         o         p\n",
       "b  0.825853  0.136707  0.575093\n",
       "c  0.185328  0.108377  0.219697\n",
       "d  0.171941  0.816225  0.274074"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>u</th>\n",
       "      <th>i</th>\n",
       "      <th>o</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.543405</td>\n",
       "      <td>0.278369</td>\n",
       "      <td>0.424518</td>\n",
       "      <td>0.844776</td>\n",
       "      <td>0.004719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.891322</td>\n",
       "      <td>0.209202</td>\n",
       "      <td>0.185328</td>\n",
       "      <td>0.108377</td>\n",
       "      <td>0.219697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.978624</td>\n",
       "      <td>0.811683</td>\n",
       "      <td>0.171941</td>\n",
       "      <td>0.816225</td>\n",
       "      <td>0.274074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          y         u         i         o         p\n",
       "a  0.543405  0.278369  0.424518  0.844776  0.004719\n",
       "c  0.891322  0.209202  0.185328  0.108377  0.219697\n",
       "d  0.978624  0.811683  0.171941  0.816225  0.274074"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>u</th>\n",
       "      <th>i</th>\n",
       "      <th>o</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0.543405</td>\n",
       "      <td>0.278369</td>\n",
       "      <td>0.424518</td>\n",
       "      <td>0.844776</td>\n",
       "      <td>0.004719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.121569</td>\n",
       "      <td>0.670749</td>\n",
       "      <td>0.825853</td>\n",
       "      <td>0.136707</td>\n",
       "      <td>0.575093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.891322</td>\n",
       "      <td>0.209202</td>\n",
       "      <td>0.185328</td>\n",
       "      <td>0.108377</td>\n",
       "      <td>0.219697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.978624</td>\n",
       "      <td>0.811683</td>\n",
       "      <td>0.171941</td>\n",
       "      <td>0.816225</td>\n",
       "      <td>0.274074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>0.431704</td>\n",
       "      <td>0.940030</td>\n",
       "      <td>0.817649</td>\n",
       "      <td>0.336112</td>\n",
       "      <td>0.175410</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          y         u         i         o         p\n",
       "a  0.543405  0.278369  0.424518  0.844776  0.004719\n",
       "b  0.121569  0.670749  0.825853  0.136707  0.575093\n",
       "c  0.891322  0.209202  0.185328  0.108377  0.219697\n",
       "d  0.978624  0.811683  0.171941  0.816225  0.274074\n",
       "e  0.431704  0.940030  0.817649  0.336112  0.175410"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a    False\n",
       "b     True\n",
       "c    False\n",
       "d    False\n",
       "e     True\n",
       "Name: i, dtype: bool"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>u</th>\n",
       "      <th>i</th>\n",
       "      <th>o</th>\n",
       "      <th>p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0.121569</td>\n",
       "      <td>0.670749</td>\n",
       "      <td>0.825853</td>\n",
       "      <td>0.136707</td>\n",
       "      <td>0.575093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>0.431704</td>\n",
       "      <td>0.940030</td>\n",
       "      <td>0.817649</td>\n",
       "      <td>0.336112</td>\n",
       "      <td>0.175410</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          y         u         i         o         p\n",
       "b  0.121569  0.670749  0.825853  0.136707  0.575093\n",
       "e  0.431704  0.940030  0.817649  0.336112  0.175410"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "np.random.seed(100)  # 固定生成的随机数不会随着每次运行变换一次\n",
    "df = pd.DataFrame(np.random.rand(5, 5), index=list(\"abcde\"), columns=list(\"yuiop\"))\n",
    "display(df)\n",
    "# 先获取行，再获取列\n",
    "display(df.loc[\"c\"][\"i\"])\n",
    "display(df.loc[\"c\",\"i\"])\n",
    "display(df.loc[\"c\"].loc[\"i\"])\n",
    "# 获取整行\n",
    "display(df.loc[\"c\"])\n",
    "# 先获取列，在获取行。\n",
    "# df[\"i\"].loc[\"a\"] = 3\n",
    "display(df[[\"i\", \"o\", \"p\"]].loc[\"b\":\"d\"])\n",
    "\n",
    "# 注意：df[索引]是对列进行操作。df[切片]是对行进行操作。\n",
    "# 获取列，永远对列进行操作。\n",
    "# df[\"a\"] # 没有a列，报错\n",
    "# df[切片]\n",
    "# 说明：df[切片]既可以根据标签索引切片，也可以根据位置索引切片。【先按标签，再按索引，行为类似与ix，故不建议使用。】\n",
    "# df[\"a\":\"c\"]\n",
    "# df[0:2]\n",
    "\n",
    "# 通过布尔数组，是对行进行操作。\n",
    "bool_array = [True, False, True, True, False]\n",
    "display(df[bool_array])\n",
    "\n",
    "# 通过标签数组，是对列进行操作。\n",
    "# lable_array = [\"y\", \"i\", \"p\"]\n",
    "# df[lable_array]\n",
    "display(df)\n",
    "# display(df > 0.5)\n",
    "# 如果布尔数组是二维结构，则True对应的位置元素原样显示，False对应位置的元素置为空值（NaN）\n",
    "# display(df[df > 0.5])\n",
    "display(df[\"i\"] > 0.5)\n",
    "display(df[df[\"i\"] > 0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>h</th>\n",
       "      <td>0.837616</td>\n",
       "      <td>0.520161</td>\n",
       "      <td>0.218272</td>\n",
       "      <td>0.134919</td>\n",
       "      <td>0.979070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.707043</td>\n",
       "      <td>0.859976</td>\n",
       "      <td>0.387173</td>\n",
       "      <td>0.250834</td>\n",
       "      <td>0.299438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>j</th>\n",
       "      <td>0.856896</td>\n",
       "      <td>0.472984</td>\n",
       "      <td>0.663277</td>\n",
       "      <td>0.805729</td>\n",
       "      <td>0.252981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.079573</td>\n",
       "      <td>0.732761</td>\n",
       "      <td>0.961397</td>\n",
       "      <td>0.953805</td>\n",
       "      <td>0.490499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.632192</td>\n",
       "      <td>0.732995</td>\n",
       "      <td>0.902410</td>\n",
       "      <td>0.162247</td>\n",
       "      <td>0.405881</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d         e\n",
       "h  0.837616  0.520161  0.218272  0.134919  0.979070\n",
       "i  0.707043  0.859976  0.387173  0.250834  0.299438\n",
       "j  0.856896  0.472984  0.663277  0.805729  0.252981\n",
       "k  0.079573  0.732761  0.961397  0.953805  0.490499\n",
       "l  0.632192  0.732995  0.902410  0.162247  0.405881"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a    0.707043\n",
       "b    0.859976\n",
       "c    0.387173\n",
       "d    0.250834\n",
       "e    0.299438\n",
       "Name: i, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a    0.707043\n",
       "b    0.859976\n",
       "c    0.387173\n",
       "d    0.250834\n",
       "e    0.299438\n",
       "Name: i, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a     23.00\n",
       "b     33.00\n",
       "c     12.00\n",
       "d    334.22\n",
       "e    200.00\n",
       "Name: p, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>i</th>\n",
       "      <td>0.707043</td>\n",
       "      <td>0.859976</td>\n",
       "      <td>0.387173</td>\n",
       "      <td>0.250834</td>\n",
       "      <td>0.299438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>k</th>\n",
       "      <td>0.079573</td>\n",
       "      <td>0.732761</td>\n",
       "      <td>0.961397</td>\n",
       "      <td>0.953805</td>\n",
       "      <td>0.490499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>l</th>\n",
       "      <td>0.632192</td>\n",
       "      <td>0.732995</td>\n",
       "      <td>0.902410</td>\n",
       "      <td>0.162247</td>\n",
       "      <td>0.405881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>p</th>\n",
       "      <td>23.000000</td>\n",
       "      <td>33.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>334.220000</td>\n",
       "      <td>200.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           a          b          c           d           e\n",
       "i   0.707043   0.859976   0.387173    0.250834    0.299438\n",
       "k   0.079573   0.732761   0.961397    0.953805    0.490499\n",
       "l   0.632192   0.732995   0.902410    0.162247    0.405881\n",
       "p  23.000000  33.000000  12.000000  334.220000  200.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(5, 5), columns=list(\"abcde\"), index=list(\"hijkl\"))\n",
    "display(df)\n",
    "# DataFrame行操作\n",
    "# 获取行 loc   iloc   ix\n",
    "# loc 根据标签获取\n",
    "# iloc 根据位置获取\n",
    "# ix 混合索引  先根据标签，然后再根据位置（不建议使用）\n",
    "display(df.loc[\"i\"])\n",
    "display(df.iloc[1])\n",
    "# 不建议使用，因为非常容易造成混淆。\n",
    "# display(df.ix[\"i\"])\n",
    "# display(df.ix[1])\n",
    "# 增加一行\n",
    "line = pd.Series([23, 33, 12., 334.22, 200], index=list(\"abcde\"), name=\"p\")\n",
    "display(line)\n",
    "df = df.append(line)\n",
    "# 删除行\n",
    "df = df.drop([\"h\", \"j\"])\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame结构\n",
    "DataFrame的一行或一列，都是Series类型的对象。对于行来说，Series对象的name属性值就是行索引名称，其内部元素的值，就是对应的列索引名称。对于列来说，Series对象的name属性值就是列索引名称，其内部元素的值，就是对应的行索引名称。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b\n",
       "0  1  2\n",
       "1  3  4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    3\n",
       "Name: a, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "Name: 0, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "Name: 0, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# DataFrame的每一行，或每一列，都是Series类型\n",
    "df = pd.DataFrame([[1, 2], [3, 4]], columns=[\"a\", \"b\"])\n",
    "display(df)\n",
    "display(type(df[\"a\"]), df[\"a\"])\n",
    "display(type(df.loc[0]), df.loc[0])  # loc根据标签索引，行标签是0\n",
    "display(type(df.iloc[0]), df.iloc[0])# iloc根据位置进行索引，第一个位置是0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame运算\n",
    "DataFrame的一行或一列都是Series类型的对象。因此，DataFrame可以近似看做是多行或多列Series构成的，Series对象支持的很多操作，对于DataFrame对象也同样适用，我们可以参考之前Series对象的操作。  \n",
    "* 转置\n",
    "* DataFrame进行运算时，会根据行索引与列索引进行对齐。当索引无法匹配时，产生空值（NaN）。如果不想产生空值，可以使用DataFrame提供的运算函数来代替运算符计算，通过fill_value参数来指定填充值。\n",
    "* DataFrame与Series混合运算。【默认Series索引匹配DataFrame的列索引，然后进行行广播。可以通过DataFrame对象的运算方法的axis参数，指定匹配方式（匹配行索引还是列索引）。】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  0  1  2\n",
       "1  3  4  5\n",
       "2  6  7  8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  0  3  6\n",
       "1  1  4  7\n",
       "2  2  5  8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15</td>\n",
       "      <td>17</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>21</td>\n",
       "      <td>23</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2\n",
       "0   9  11  13\n",
       "1  15  17  19\n",
       "2  21  23  25"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  0  1  2\n",
       "1  3  4  5\n",
       "2  6  7  8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    2   3   4\n",
       "1   9  10  11\n",
       "2  12  13  14\n",
       "3  15  16  17"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1     2   3   4\n",
       "0 NaN NaN   NaN NaN NaN\n",
       "1 NaN NaN  14.0 NaN NaN\n",
       "2 NaN NaN  20.0 NaN NaN\n",
       "3 NaN NaN   NaN NaN NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000.0</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>1002.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1003.0</td>\n",
       "      <td>1004.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>1010.0</td>\n",
       "      <td>1011.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1006.0</td>\n",
       "      <td>1007.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>1013.0</td>\n",
       "      <td>1014.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1015.0</td>\n",
       "      <td>1016.0</td>\n",
       "      <td>1017.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        0       1       2       3       4\n",
       "0  1000.0  1001.0  1002.0     NaN     NaN\n",
       "1  1003.0  1004.0    14.0  1010.0  1011.0\n",
       "2  1006.0  1007.0    20.0  1013.0  1014.0\n",
       "3     NaN     NaN  1015.0  1016.0  1017.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.arange(9).reshape(3, 3))\n",
    "# 转置\n",
    "display(df, df.T)\n",
    "print('-'*100)\n",
    "df2 = pd.DataFrame(np.arange(9, 18).reshape(3, 3))\n",
    "display(df + df2)\n",
    "print('-'*100)\n",
    "df3 = pd.DataFrame(np.arange(9, 18).reshape(3, 3), index=[1, 2, 3], columns=[2, 3, 4])\n",
    "display(df, df3)\n",
    "print('-'*100)\n",
    "# DataFrame进行计算时，会根据标签进行对齐，如果标签无法对齐，则会产生空值（NaN）。\n",
    "display(df + df3)\n",
    "# 如果不想结果为NaN，可以采用DataFrame提供的计算方法，来代替运算符的计算。\n",
    "display(df.add(df3, fill_value=1000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2\n",
       "0  0  1  2\n",
       "1  3  4  5\n",
       "2  6  7  8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0    1000\n",
       "1    2000\n",
       "2    3000\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1000</td>\n",
       "      <td>2001</td>\n",
       "      <td>3002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1003</td>\n",
       "      <td>2004</td>\n",
       "      <td>3005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1006</td>\n",
       "      <td>2007</td>\n",
       "      <td>3008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      0     1     2\n",
       "0  1000  2001  3002\n",
       "1  1003  2004  3005\n",
       "2  1006  2007  3008"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1    1000\n",
       "2    2000\n",
       "3    3000\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1001.0</td>\n",
       "      <td>2002.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1004.0</td>\n",
       "      <td>2005.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1007.0</td>\n",
       "      <td>2008.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0       1       2   3\n",
       "0 NaN  1001.0  2002.0 NaN\n",
       "1 NaN  1004.0  2005.0 NaN\n",
       "2 NaN  1007.0  2008.0 NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1003.0</td>\n",
       "      <td>1004.0</td>\n",
       "      <td>1005.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2006.0</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>2008.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        0       1       2\n",
       "0     NaN     NaN     NaN\n",
       "1  1003.0  1004.0  1005.0\n",
       "2  2006.0  2007.0  2008.0\n",
       "3     NaN     NaN     NaN"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Series与DataFrame混合运算，运算时会根据标签进行对齐。\n",
    "df = pd.DataFrame(np.arange(9).reshape(3, 3))\n",
    "s = pd.Series([1000, 2000, 3000])\n",
    "display(df, s)\n",
    "display(df + s)\n",
    "\n",
    "# 当进行混合运算时，默认情况下，Series用自己的标签匹配DataFrame的列标签，然后进行运算。\n",
    "# 如果需要匹配行标签，则可以使用axis进行设置。\n",
    "# display(df.add(s, axis=\"index\"))\n",
    "s2 = pd.Series([1000, 2000, 3000], index=[1, 2, 3])\n",
    "display(s2)\n",
    "display(df.add(s2)) # 默认按列计算\n",
    "display(df.add(s2,axis = 0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 排序\n",
    "### 索引排序\n",
    "Series与DataFrame对象可以使用sort_index方法对索引进行排序。DataFrame对象在排序时，还可以通过axis参数来指定轴（行索引还是列索引）。也可以通过ascending参数指定升序还是降序。\n",
    "### 值排序\n",
    "Series与DataFrame对象可以使用sort_values方法对列或行的值进行排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>6</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   6  4  5\n",
       "3  0  1  2\n",
       "1  3  4  5\n",
       "2  6  7  8"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'默认根据行索引进行排序:'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>6</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   6  4  5\n",
       "1  3  4  5\n",
       "2  6  7  8\n",
       "3  0  1  2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'根据列索引进行排序:'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   4  5  6\n",
       "3  1  2  0\n",
       "1  4  5  3\n",
       "2  7  8  6"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'默认为升序排序，可以指定为降序排序，按列索引：'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>6</th>\n",
       "      <th>5</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   6  5  4\n",
       "3  0  2  1\n",
       "1  3  5  4\n",
       "2  6  8  7"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1    2\n",
       "0  1  3  300\n",
       "1  0  5  100\n",
       "2  1  3  400"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'按照第二列默认升序排序:'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1    2\n",
       "0  1  3  300\n",
       "2  1  3  400\n",
       "1  0  5  100"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'按照第二列降序排序:'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1    2\n",
       "1  0  5  100\n",
       "0  1  3  300\n",
       "2  1  3  400"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'按照第三行降序排序:'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1    2\n",
       "2  1  3  400\n",
       "0  1  3  300\n",
       "1  0  5  100"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.arange(9).reshape(3, 3), index=[3, 1, 2], columns=[6,4,5])\n",
    "display(df)\n",
    "# 默认根据行索引进行排序。\n",
    "display('默认根据行索引进行排序:',df.sort_index())\n",
    "# 根据列索引进行排序\n",
    "display('根据列索引进行排序:',df.sort_index(1))\n",
    "# 就地修改，不会返回修改后的结果。\n",
    "# df.sort_index(inplace=True)\n",
    "# 默认为升序排序，可以指定为降序排序。\n",
    "display('默认为升序排序，可以指定为降序排序，按列索引：',df.sort_index(ascending=False, axis=1))\n",
    "\n",
    "# 值排序\n",
    "df = pd.DataFrame([[1, 3, 300], [0, 5, 100], [1, 3, 400]])\n",
    "display(df)\n",
    "display('按照第二列默认升序排序:',df.sort_values(1))  # 1代表第二列，按照第二列默认升序排序\n",
    "display('按照第二列降序排序:',df.sort_values(1,ascending=False))\n",
    "\n",
    "display('按照第三行降序排序:',df.sort_values(2,axis = 0,ascending = False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引对象\n",
    "Series(DataFrame)的index或者DataFrame的columns就是一个索引对象。\n",
    "* 索引对象可以向数组那样进行索引访问。\n",
    "* 索引对象是不可修改的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ind = pd.Index([1, 2, 3])\n",
    "display(ind[0])\n",
    "# 索引对象的元素不支持修改。\n",
    "#ind[0] = 30\n",
    "#df.index = []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 统计相关方法\n",
    "* mean / sum / count\n",
    "* max / min\n",
    "* cumsum / cumprod\n",
    "* argmax / argmin\n",
    "* idxmax / idxmin\n",
    "* var / std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.485414</td>\n",
       "      <td>0.966693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.211348</td>\n",
       "      <td>0.411648</td>\n",
       "      <td>0.989666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.028412</td>\n",
       "      <td>0.701327</td>\n",
       "      <td>0.025172</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          8         9         7\n",
       "0       NaN  0.485414  0.966693\n",
       "2  0.211348  0.411648  0.989666\n",
       "3  0.028412  0.701327  0.025172"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "8    2\n",
       "9    3\n",
       "7    3\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.rand(3, 3), index=[0, 2, 3], columns=[8, 9, 7])\n",
    "df[8].loc[0] = np.nan\n",
    "display(df)\n",
    "# count统计不包含nan。\n",
    "display(df.count())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 其他\n",
    "* unique\n",
    "* value_counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    3\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([3, 1, 2], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3    2\n",
       "2    1\n",
       "1    1\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Series unique 去掉重复的元素。（没有排序的功能）\n",
    "s = pd.Series([3, 1, 2, 3])\n",
    "display(s)\n",
    "print('-----')\n",
    "display(s.unique())\n",
    "print('-----')\n",
    "# 按Series的值进行分组，分别统计该值出现的数量。\n",
    "display(s.value_counts())"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
